package cn.yx.tool.config.service;

import cn.yx.common.core.annotation.config.SysConfig;
import cn.yx.common.core.annotation.config.SysConfigName;
import cn.yx.common.core.annotation.config.SysConfigParam;
import cn.yx.common.core.constant.FileConstant;
import cn.yx.common.core.util.io.FileUtil;
import cn.yx.common.core.util.io.IoUtil;
import cn.yx.common.core.util.reflect.ClassUtil;
import cn.yx.tool.config.properties.SysConfigGeneratorProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

/**
 * <p>系统配置SQL生成服务</p>
 *
 * @author Wgssmart
 */
@Service
@Slf4j
public class SysConfigGeneratorService {

    private final SysConfigGeneratorProperties sysConfigGeneratorProperties;

    public SysConfigGeneratorService(SysConfigGeneratorProperties sysConfigGeneratorProperties) {
        this.sysConfigGeneratorProperties = sysConfigGeneratorProperties;
    }

    /**
     * 根据配置类指定的注解生成insert sql文件
     */
    public void generateSysConfigSql() {
        String dir = FileUtil.mkdirs(sysConfigGeneratorProperties.getBaseDir() + sysConfigGeneratorProperties.getSaveDir());
        for (String jarFilePath : sysConfigGeneratorProperties.getJarFilePath()) {
            File jarFile = new File(sysConfigGeneratorProperties.getBaseDir() + jarFilePath);
            List<Class<?>> classList = ClassUtil.getClassesFromOutJar(jarFile, SysConfig.class);
            for (Class<?> clazz : classList) {
                StringBuilder sqlBuilder = new StringBuilder();
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    sqlBuilder.append(createSql(field));
                }
                IoUtil.writeText(sqlBuilder.toString(), dir + FileConstant.SEPARATOR + clazz.getSimpleName() + ".sql");
            }
        }

    }

    /**
     * config配置类生成sql语句
     *
     * @param field
     * @return
     */
    private String createSql(Field field) {
        SysConfigName sysConfigName = field.getAnnotation(SysConfigName.class);
        SysConfigParam sysConfigParam = field.getAnnotation(SysConfigParam.class);
        try {
            String configSql = "";
            String configDetailSql = "";
            if (Objects.nonNull(sysConfigName)) {
                configSql = sysConfigGeneratorProperties.getSysConfigSqlTemplate()
                        .replaceAll(":name", "'" + field.get(null).toString() + "'")
                        .replaceAll(":description", "'" + sysConfigName.description() + "'")
                        + ";\n\n";
            }
            if (Objects.nonNull(sysConfigParam)) {
                configDetailSql = sysConfigGeneratorProperties.getSysConfigDetailSqlTemplate()
                        .replaceAll(":sysConfigId", "1000")
                        .replaceAll(":parameter", "'" + field.getName() + "'")
                        .replaceAll(":value", "'" + sysConfigParam.defaultValue() + "'")
                        .replaceAll(":description", "'" + sysConfigParam.description() + "'")
                        + ";\n";
            }
            return configSql + configDetailSql;
        } catch (IllegalAccessException e) {
            log.error("create sql error: {}", e.getMessage());
            return "";
        }
    }

}
